package com.binhthuan.catechist.server.bus;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.binhthuan.catechist.server.QueryObject;
import com.binhthuan.catechist.server.domain.AbstractObject;
import com.binhthuan.catechist.server.domain.Date;
import com.binhthuan.catechist.server.util.HibernateUtil;

public class DateBUS extends AbstractBUS {

    public Date getSchoolDateById(int id) {
        return (Date) get(Date.class, id);
    }

    public Date getSchoolDateByDate(java.util.Date d) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Date result = null;
        try {
            transaction = session.beginTransaction();
            Query query = session.createQuery("from Date where dateOfYear = :dateOfYear");
            query.setDate("dateOfYear", d);
            result = (Date) query.uniqueResult();
            transaction.commit();
        } catch (HibernateException he) {
            he.printStackTrace();
            transaction.rollback();
        } finally {
            session.close();
        }

        return result;
    }

    @SuppressWarnings("unchecked")
    public QueryObject getSchoolDateBySchoolYear(int yearId, int start, int limit) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        QueryObject result = new QueryObject();
        List<AbstractObject> records = null;
        try {
            transaction = session.beginTransaction();
            Criteria criteria = session.createCriteria(Date.class).add(Restrictions.eq("year.id", yearId)).addOrder(Order.asc("dateOfYear"));

            criteria.setProjection(Projections.rowCount());
            result.setTotalRows(Integer.parseInt(criteria.list().get(0).toString()));

            criteria.setProjection(null);
            criteria.setResultTransformer(Criteria.ROOT_ENTITY);
            if (limit > 0) {
                criteria = criteria.setFirstResult(start).setMaxResults(limit);
            }

            records = new ArrayList<AbstractObject>(criteria.list());

            transaction.commit();
        } catch (HibernateException he) {
            he.printStackTrace();
            transaction.rollback();
        } finally {
            session.close();
        }

        result.setStartRows(start);
        int endRow = start;
        if (records != null) {
            endRow += records.size();
            result.setData(records);
        }

        result.setEndRow(endRow);

        return result;
    }

    public boolean deleteDate(int id) {
        return delete(Date.class, id);
    }
}
